package test.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.CharBuffer;

import hyl.core.MyCharset;
import hyl.core.MyFun;
import hyl.core.fun.MyByte;
import hyl.core.net.BioServer;
import hyl.core.net.IReader;

import hyl.core.net.NioServer;
import hyl.core.run.IDoEachT1;
import hyl.core.run.IReady;
import jnr.ffi.Struct.int16_t;

public class testServer {

	public static void main(String[] args) throws IOException {
		/* 服务器地址 */

		// testnio();
		//testbio();
		//testlambdabio();
		testping();
	}

	static class Nio接收 implements IDoEachT1<IReader, byte[]> {
		public boolean isOver(IReader sc, byte[] b数据) {
	
			System.out.println(MyFun.bytes2Str(b数据, MyCharset._UTF8));
			try {
				sc.send(b数据);
			} catch (IOException e) {
			
				e.printStackTrace();
			}
			return false;
		}
	};

	public static void testnio() throws IOException {
		NioServer _srv = new NioServer(9090);
		_srv.setReaderHandler(() -> {
			return new Nio接收();
		});
		_srv.start();
	}

	static class Bio接收 implements IDoEachT1<IReader, byte[]> {
		int i = 0;
		public boolean isOver(IReader skt, byte[] b数据) {
			i++;
			String s数据 = MyFun.bytes2Str(b数据, MyCharset._UTF8);
			System.out.println(s数据);
			try {
				skt.send(MyFun.str2Bytes("反馈:" + i + "-" + s数据, MyCharset._UTF8));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;
		}
	};

	public static void testbio() throws IOException {
		BioServer _srv = new BioServer(9090);
		// 隐式实现ready
		_srv.setReaderHandler(() -> {
			return new Bio接收();
		});
		_srv.start();
	}

	// 写法2 说明在静态方法中的 lambda 实际类似单例模式, 不适用于并发,演绎如下例子
	public static void testlambdabio() throws IOException {
		BioServer _srv = new BioServer(9090);
		// 隐式实现ready
		IReady<IDoEachT1<IReader,byte[]>> c=() -> {
			IDoEachT1<IReader,byte[]> b = (skt, b数据) -> {
				String s数据 = MyFun.bytes2Str(b数据, MyCharset._UTF8);
				// String iString=this.toString();
				System.out.println(s数据);
				try {
					skt.send(MyFun.str2Bytes("反馈:" + s数据, MyCharset._UTF8));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return false;
			};
			System.out.println(b);
			return (IDoEachT1<IReader, byte[]>) b;
		};		
		_srv.setReaderHandler(c);
		_srv.start();
	}
	
	 public static void testping() {
	        try {
	            // 创建socket服务端，端口为15000
	            ServerSocket serverSocket = new ServerSocket(15000);
	            CharBuffer cb=       CharBuffer.allocate(20);
	            byte[] bb=new byte[20]; 
	            while (true){
	                //此方法为阻塞方法，用户等待客户端连接
	               Socket socket =  serverSocket.accept();
	                InputStream inputStream = socket.getInputStream();
	                OutputStream outputStream = socket.getOutputStream();
	              //  InputStreamReader in = new InputStreamReader(inputStream);
	             //   BufferedReader reader = new BufferedReader(in);
	                boolean flag =true;
	          
	                while (flag){
	                	//in.read(cb);
	                	//  MyFun.print("说1：",cb);
	                	//cb.flip();
	                	int i =inputStream.read(bb);
	                	if ( i>0) {
	                		 MyFun.print("说2：",i,MyByte.bytes2str(bb, 0, i) );
						}
	                  
	                   // String info = reader.readLine();
	              
//	                    if(info.endsWith("end")){
//	                        System.out.println("客户端说2："+info);
//	                        flag = false;
//	                    }
	                }
	                PrintWriter writer = new PrintWriter(outputStream);
	                writer.write("我是服务端，hello");
	                writer.flush();

	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }

}
